package com.uzhie.util;

import com.github.pagehelper.Page;
import com.uzhie.web.bean.*;

import java.util.List;

/**
 * Created by TangFei on 2018/7/20.
 */
public class PayloadBeanUtil {

    private static PayloadBeanUtil instance;

    private PayloadBeanUtil() {
    }

    public static PayloadBeanUtil getInstance() {
        if (instance == null) {
            synchronized (PayloadBeanUtil.class) {
                if (instance == null) {
                    instance = new PayloadBeanUtil();
                }
            }
        }
        return instance;
    }

    public <T> void build(Class<T> clazz) throws IllegalAccessException, InstantiationException {
        T instance = clazz.newInstance();
        if (instance instanceof ResponseBodyBean) {
            ResponseBodyBean bodyBean = (ResponseBodyBean) instance;
            buildPayload(bodyBean);
        }
    }

    public <T> void buildPayload(PayloadBean<T> payloadBean) {

    }

    /**
     * 不分页controller响应消息处理
     *
     * @param clazz  ? extends ResponseBodyBean
     * @param result service处理结果
     * @param e      异常信息
     * @param <T>
     * @return
     * @throws IllegalAccessException
     * @throws InstantiationException
     */
    public <T> PayloadBean buildPayload(Class<? extends ResponseBodyBean> clazz, T result, Exception e) throws IllegalAccessException, InstantiationException {
        ErrorBean errorBean = new ErrorBean();
        errorBean.setCode(500);
        errorBean.setMsg(e.getMessage());
        return buildPayload(clazz, result, errorBean);
    }

    /**
     * 不分页controller响应消息处理
     *
     * @param clazz     ? extends ResponseBodyBean
     * @param result    service处理结果
     * @param errorBean 异常对象
     * @param <T>
     * @return
     * @throws IllegalAccessException
     * @throws InstantiationException
     */
    public <T> PayloadBean buildPayload(Class<? extends ResponseBodyBean> clazz, T result, ErrorBean errorBean) throws IllegalAccessException, InstantiationException {
        ResponseBodyBean<T> bodyBean = clazz.newInstance();
        bodyBean.setError(errorBean);
        bodyBean.setResult(result);
        return bodyBean;
    }

    /**
     * 分页controller响应消息体处理
     *
     * @param clazz     需要返回的类
     * @param result
     * @param pageBean
     * @param errorBean
     * @param <T>
     * @return
     * @throws IllegalAccessException
     * @throws InstantiationException
     */
    public <T> PayloadBean buildPayload(Class<? extends ResponseBodyBean> clazz, List<T> result, PageBean pageBean, ErrorBean errorBean) throws IllegalAccessException, InstantiationException {
        ResponseBodyBean<List<T>> bodyBean = clazz.newInstance();
        if (null == result) {
            bodyBean.setResult(result);
            pageBean.setCountPage(0);
            pageBean.setCountSize(0);
            bodyBean.setPage(pageBean);
            bodyBean.setError(errorBean);
        } else {
            Page<T> tPage = (Page<T>) result;
            bodyBean.setResult(result);
            pageBean.setCountPage(tPage.getPages());
            pageBean.setCountSize(tPage.getTotal());
            bodyBean.setPage(pageBean);
        }
        return bodyBean;
    }

    public <T> PayloadBean buildPayload(Class<? extends ResponseBean> clazz, RequestBean requestBean, ResponseBodyBean<T> responseBodyBean, int code, String msg) throws IllegalAccessException, InstantiationException {
        ResponseBean responseBean = clazz.newInstance();
        responseBean.setResponseCode(code);
        responseBean.setResponseMsg(msg);
        responseBean.setRequestId(requestBean.getRequestId());
        responseBean.setRequestMethod(requestBean.getRequestMethod());
        responseBean.setResponseBody(responseBodyBean);
        return responseBean;
    }

}
